package com.restaurant.springboot.repositories;

import com.restaurant.springboot.entity.CommentEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

public interface CommentRepository  extends JpaRepository<CommentEntity,Integer> {

    @Query(value = "SELECT cm.id,\n" +
            "cm.user_id,\n" +
            "cm.content,\n" +
            "cm.order_id,\n" +
            "cm.time,\n" +
            "cm.praise,\n" +
            "u.open_id,\n" +
            "u.name,\n" +
            "u.photo_url\n" +
            "FROM\n" +
            "comment cm\n" +
            "INNER JOIN user u ON cm.user_id = u.id where cm.id=:commentId\n",nativeQuery = true)
    Map<String,Object> getUserCommentById(@Param("commentId") int commentId);

    @Query(value = "SELECT cm.id,\n" +
            "cm.user_id,\n" +
            "cm.content,\n" +
            "cm.order_id,\n" +
            "cm.time,\n" +
            "cm.praise,\n" +
            "u.open_id,\n" +
            "u.name,\n" +
            "u.photo_url\n" +
            "FROM\n" +
            "comment cm\n" +
            "INNER JOIN user u ON cm.user_id = u.id\n",nativeQuery = true)
    List<Map<String,Object>> getAllUserComments(Pageable pageable);


    boolean existsByOrderId(int orderId);

}